//
//  SGReceive.m
//  YaKe
//
//  Created by liu chao on 15/10/9.
//  Copyright (c) 2015年 ssyx. All rights reserved.
//

#import "SGReceive.h"
#import "MJExtension.h"

@implementation SGReceive

+ (instancetype)receiveWithResponseObject:(id)responseObject dataWord:(NSString *)dataWord dataClass:(Class)clazz{
    SGReceive *receive = [[self alloc] init];
    
    if ([responseObject isKindOfClass:[NSDictionary class]]) {
        //状态码
        NSString *status = responseObject[@"status"];
        NSString *msg  = responseObject[@"msg"];
        //data
        id data = responseObject[@"data"][dataWord];
        id dataModel;
        if ([data isKindOfClass:[NSArray class]]) {
            dataModel = [clazz mj_objectArrayWithKeyValuesArray:data];
        } else if([data isKindOfClass:[NSDictionary class]]){
            dataModel = [clazz mj_objectWithKeyValues:data];
        }
        
        receive.status = status;
        receive.msg  = msg;
        receive.data = dataModel;
    } else {
        receive.status = @"-100";
        receive.msg  = @"获取数据失败";
        receive.data = nil;
    }
    
    return receive;
}

+ (instancetype)receiveWithResponseObject:(id)responseObject dataClass:(Class)clazz{
    SGReceive *receive = [[self alloc] init];
    
    if ([responseObject isKindOfClass:[NSDictionary class]]) {
        //状态码
        NSString *status = responseObject[@"status"];
        NSString *msg  = responseObject[@"msg"];
        //data
        id data = responseObject[@"data"];
        id dataModel;
        if ([data isKindOfClass:[NSArray class]]) {
            dataModel = [clazz mj_objectArrayWithKeyValuesArray:data];
        } else if([data isKindOfClass:[NSDictionary class]]){
            dataModel = [clazz mj_objectWithKeyValues:data];
        }
        
        receive.status = status;
        receive.msg  = msg;
        receive.data = dataModel;
    } else {
        receive.status = @"-100";
        receive.msg  = @"获取数据失败";
        receive.data = nil;
    }
    
    return receive;
}

@end
